package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.gaussdbfornosql.v3.GaussDBforNoSQLClient;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.BatchTagActionRequest;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.BatchTagActionRequestBody;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.BatchTagActionResponse;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.BatchTagActionTagOption;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.ListInstanceTagsRequest;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.ListInstanceTagsResponse;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.ListInstancesByResourceTagsRequest;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.ListInstancesByResourceTagsResponse;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.ListInstancesByTagsRequestBody;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.ListInstancesRequest;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.ListInstancesResponse;
import com.huaweicloud.sdk.gaussdbfornosql.v3.model.TagOption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class NoSqlInstanceTagDemo {
    private static final Logger logger = LoggerFactory.getLogger(NoSqlInstanceTagDemo.class.getName());

    /**
     * args[0] = "<YOUR IAM_END_POINT>"
     * args[1] = "<YOUR END_POINT>"
     * args[2] = "<YOUR AK>"
     * args[3] = "<YOUR SK>"
     * args[4] = "<YOUR INSTANCE_ID>"
     * args[5] = "<YOUR REGION_ID>"
     *
     * @param args
     */
    public static void main(String[] args) {
        if (args.length != 6) {
            logger.info("Illegal Arguments");
        }

        String iamEndpoint = args[0];
        String endpoint = args[1];

        String ak = args[2];
        String sk = args[3];

        String instanceId = args[4];
        String regionId = args[5];

        ICredential auth = new BasicCredentials()
                .withIamEndpoint(iamEndpoint)
                .withAk(ak)
                .withSk(sk);

        GaussDBforNoSQLClient client = GaussDBforNoSQLClient.newBuilder()
                .withCredential(auth)
                .withRegion(new Region(regionId, endpoint))
                .build();

        // 获取已有实例
        getInstanceList(client);

        // 查询实例标签
        getInstanceTags(client, instanceId);

        // 设置实例标签
        setInstanceTags(client, instanceId);

        // 根据标签查询实例
        getInstanceByResourceTags(client);
    }

    public static void getInstanceList(GaussDBforNoSQLClient client) {
        ListInstancesRequest request = new ListInstancesRequest();
        try {
            ListInstancesResponse response = client.listInstances(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    public static void getInstanceTags(GaussDBforNoSQLClient client, String instanceId) {
        ListInstanceTagsRequest request = new ListInstanceTagsRequest();
        request.setInstanceId(instanceId);
        try {
            ListInstanceTagsResponse response = client.listInstanceTags(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    public static void setInstanceTags(GaussDBforNoSQLClient client, String instanceId) {
        BatchTagActionRequest request = new BatchTagActionRequest();
        request.setInstanceId(instanceId);
        BatchTagActionRequestBody body = new BatchTagActionRequestBody();
        BatchTagActionTagOption tagsItem = new BatchTagActionTagOption();
        tagsItem.withKey("test_key_new").withValue("test_value_new");
        body.withAction(BatchTagActionRequestBody.ActionEnum.CREATE);
        body.withTags(Arrays.asList(tagsItem));
        request.setBody(body);
        try {
            BatchTagActionResponse response = client.batchTagAction(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }

    public static void getInstanceByResourceTags(GaussDBforNoSQLClient client) {
        ListInstancesByResourceTagsRequest request = new ListInstancesByResourceTagsRequest();
        ListInstancesByTagsRequestBody body = new ListInstancesByTagsRequestBody();
        TagOption tagOption = new TagOption();
        tagOption.withKey("test_key_new").withValues(Arrays.asList("test_value_new"));
        body.setTags(Arrays.asList(tagOption));
        body.setAction(ListInstancesByTagsRequestBody.ActionEnum.FILTER);
        request.setBody(body);
        try {
            ListInstancesByResourceTagsResponse response = client.listInstancesByResourceTags(request);
            logger.info(response.toString());
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        }
    }
}
